package com.mazaiting;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class AddHeader {
	public static void main(String[] args) throws IOException {
		// 文件路径
		File fileDir = new File("E:\\test\\blog_source_code\\source\\_posts");
		File[] listFilesSub = fileDir.listFiles();
		for (File fileSub : listFilesSub) {
			File[] listFiles = fileSub.listFiles();
			for (File file : listFiles) {
				// 删除私密文件
				if (file.getName().equals("账号密码.md")) {
					file.delete();
					continue;
				}
				// 读取文件
				BufferedReader reader = new BufferedReader(new FileReader(file));
				StringBuilder builder = new StringBuilder();
				builder.append("---\n")
					.append("title: ").append(file.getName().substring(0, file.getName().length() - 3)).append("\n")
					.append("---\n");
				String temp=null;
				while ((temp = reader.readLine()) != null) {
					builder.append(temp).append("\n");	
				}
				reader.close();
				// 写入文件
				BufferedWriter writer = new BufferedWriter(new FileWriter(file));
				writer.write(builder.toString());
				writer.close();	
			}
		}
	}
}
